home *** CD-ROM | disk | FTP | other *** search
/ Risc World 5 / Risc World 5.iso / SOFTWARE / Issue5 / PD / DIRSYNC / LegalStuff / gnudiff / context.c < prev    next >
C/C++ Source or Header  |  2004-12-19  |  13KB  |  483 lines

  1. /* Context-format output routines for GNU DIFF.
  2.  
  3.    Copyright (C) 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1998, 2001,
  4.    2002 Free Software Foundation, Inc.
  5.  
  6.    This file is part of GNU DIFF.
  7.  
  8.    GNU DIFF is free software; you can redistribute it and/or modify
  9.    it under the terms of the GNU General Public License as published by
  10.    the Free Software Foundation; either version 2, or (at your option)
  11.    any later version.
  12.  
  13.    GNU DIFF is distributed in the hope that it will be useful,
  14.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.    GNU General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; see the file COPYING.
  20.    If not, write to the Free Software Foundation,
  21.    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  22.  
  23. #include "diff.h"
  24. #include <inttostr.h>
  25. #include <regex.h>
  26.  
  27. #ifdef ST_MTIM_NSEC
  28. # define TIMESPEC_NS(timespec) ((timespec).ST_MTIM_NSEC)
  29. #else
  30. # define TIMESPEC_NS(timespec) 0
  31. #endif
  32.  
  33. size_t nstrftime (char *, size_t, char const *, struct tm const *, int, int);
  34.  
  35. static char const *find_function (char const * const *, lin);
  36. static struct change *find_hunk (struct change *);
  37. static void mark_ignorable (struct change *);
  38. static void pr_context_hunk (struct change *);
  39. static void pr_unidiff_hunk (struct change *);
  40.  
  41. /* Last place find_function started searching from.  */
  42. static lin find_function_last_search;
  43.  
  44. /* The value find_function returned when it started searching there.  */
  45. static lin find_function_last_match;
  46.  
  47. /* Print a label for a context diff, with a file name and date or a label.  */
  48.  
  49. static void
  50. print_context_label (char const *mark,
  51.              struct file_data *inf,
  52.              char const *label)
  53. {
  54.   if (label)
  55.     fprintf (outfile, "%s %s\n", mark, label);
  56.   else
  57.     {
  58.       char buf[MAX (INT_STRLEN_BOUND (int) + 32,
  59.             INT_STRLEN_BOUND (time_t) + 11)];
  60.       struct tm const *tm = localtime (&inf->stat.st_mtime);
  61.       int nsec = TIMESPEC_NS (inf->stat.st_mtim);
  62.       if (! (tm && nstrftime (buf, sizeof buf, time_format, tm, 0, nsec)))
  63.     {
  64.       long sec = inf->stat.st_mtime;
  65.       verify (info_preserved, sizeof inf->stat.st_mtime <= sizeof sec);
  66.       sprintf (buf, "%ld.%.9d", sec, nsec);
  67.     }
  68. #ifdef __riscos
  69.       fprintf (outfile, "%s %s\t%s\n", mark, inf->unixname, buf);
  70. #else
  71.       fprintf (outfile, "%s %s\t%s\n", mark, inf->name, buf);
  72. #endif
  73.     }
  74. }
  75.  
  76. /* Print a header for a context diff, with the file names and dates.  */
  77.  
  78. void
  79. print_context_header (struct file_data inf[], bool unidiff)
  80. {
  81.   if (unidiff)
  82.     {
  83.       print_context_label ("---", &inf[0], file_label[0]);
  84.       print_context_label ("+++", &inf[1], file_label[1]);
  85.     }
  86.   else
  87.     {
  88.       print_context_label ("***", &inf[0], file_label[0]);
  89.       print_context_label ("---", &inf[1], file_label[1]);
  90.     }
  91. }
  92.  
  93. /* Print an edit script in context format.  */
  94.  
  95. void
  96. print_context_script (struct change *script, bool unidiff)
  97. {
  98.   if (ignore_blank_lines || ignore_regexp.fastmap)
  99.     mark_ignorable (script);
  100.   else
  101.     {
  102.       struct change *e;
  103.       for (e = script; e; e = e->link)
  104.     e->ignore = 0;
  105.     }
  106.  
  107.   find_function_last_search = - files[0].prefix_lines;
  108.   find_function_last_match = LIN_MAX;
  109.  
  110.   if (unidiff)
  111.     print_script (script, find_hunk, pr_unidiff_hunk);
  112.   else
  113.     print_script (script, find_hunk, pr_context_hunk);
  114. }
  115.  
  116. /* Print a pair of line numbers with a comma, translated for file FILE.
  117.    If the second number is not greater, use the first in place of it.
  118.  
  119.    Args A and B are internal line numbers.
  120.    We print the translated (real) line numbers.  */
  121.  
  122. static void
  123. print_context_number_range (struct file_data const *file, lin a, lin b)
  124. {
  125.   long trans_a, trans_b;
  126.   translate_range (file, a, b, &trans_a, &trans_b);
  127.  
  128.   /* We can have B <= A in the case of a range of no lines.
  129.      In this case, we should print the line number before the range,
  130.      which is B.
  131.  
  132.      POSIX 1003.1-2001 requires two line numbers separated by a comma
  133.      even if the line numbers are the same.  However, this does not
  134.      match existing practice and is surely an error in the
  135.      specification.  */
  136.  
  137.   if (trans_b <= trans_a)
  138.     fprintf (outfile, "%ld", trans_b);
  139.   else
  140.     fprintf (outfile, "%ld,%ld", trans_a, trans_b);
  141. }
  142.  
  143. /* Print FUNCTION in a context header.  */
  144. static void
  145. print_context_function (FILE *out, char const *function)
  146. {
  147.   int i;
  148.   putc (' ', out);
  149.   for (i = 0; i < 40 && function[i] != '\n'; i++)
  150.     continue;
  151.   fwrite (function, 1, i, out);
  152. }
  153.  
  154. /* Print a portion of an edit script in context format.
  155.    HUNK is the beginning of the portion to be printed.
  156.    The end is marked by a `link' that has been nulled out.
  157.  
  158.    Prints out lines from both files, and precedes each
  159.    line with the appropriate flag-character.  */
  160.  
  161. static void
  162. pr_context_hunk (struct change *hunk)
  163. {
  164.   lin first0, last0, first1, last1, i;
  165.   char const *prefix;
  166.   char const *function;
  167.   FILE *out;
  168.  
  169.   /* Determine range of line numbers involved in each file.  */
  170.  
  171.   enum changes changes = analyze_hunk (hunk, &first0, &last0, &first1, &last1);
  172.   if (! changes)
  173.     return;
  174.  
  175.   /* Include a context's width before and after.  */
  176.  
  177.   i = - files[0].prefix_lines;
  178.   first0 = MAX (first0 - context, i);
  179.   first1 = MAX (first1 - context, i);
  180.   if (last0 < files[0].valid_lines - context)
  181.     last0 += context;
  182.   else
  183.     last0 = files[0].valid_lines - 1;
  184.   if (last1 < files[1].valid_lines - context)
  185.     last1 += context;
  186.   else
  187.     last1 = files[1].valid_lines - 1;
  188.  
  189.   /* If desired, find the preceding function definition line in file 0.  */
  190.   function = 0;
  191.   if (function_regexp.fastmap)
  192.     function = find_function (files[0].linbuf, first0);
  193.  
  194.   begin_output ();
  195.   out = outfile;
  196.  
  197.   fprintf (out, "***************");
  198.  
  199.   if (function)
  200.     print_context_function (out, function);
  201.  
  202.   fprintf (out, "\n*** ");
  203.   print_context_number_range (&files[0], first0, last0);
  204.   fprintf (out, " ****\n");
  205.  
  206.   if (changes & OLD)
  207.     {
  208.       struct change *next = hunk;
  209.  
  210.       for (i = first0; i <= last0; i++)
  211.     {
  212.       /* Skip past changes that apply (in file 0)
  213.          only to lines before line I.  */
  214.  
  215.       while (next && next->line0 + next->deleted <= i)
  216.         next = next->link;
  217.  
  218.       /* Compute the marking for line I.  */
  219.  
  220.       prefix = " ";
  221.       if (next && next->line0 <= i)
  222.         /* The change NEXT covers this line.
  223.            If lines were inserted here in file 1, this is "changed".
  224.            Otherwise it is "deleted".  */
  225.         prefix = (next->inserted > 0 ? "!" : "-");
  226.  
  227.       print_1_line (prefix, &files[0].linbuf[i]);
  228.     }
  229.     }
  230.  
  231.   fprintf (out, "--- ");
  232.   print_context_number_range (&files[1], first1, last1);
  233.   fprintf (out, " ----\n");
  234.  
  235.   if (changes & NEW)
  236.     {
  237.       struct change *next = hunk;
  238.  
  239.       for (i = first1; i <= last1; i++)
  240.     {
  241.       /* Skip past changes that apply (in file 1)
  242.          only to lines before line I.  */
  243.  
  244.       while (next && next->line1 + next->inserted <= i)
  245.         next = next->link;
  246.  
  247.       /* Compute the marking for line I.  */
  248.  
  249.       prefix = " ";
  250.       if (next && next->line1 <= i)
  251.         /* The change NEXT covers this line.
  252.            If lines were deleted here in file 0, this is "changed".
  253.            Otherwise it is "inserted".  */
  254.         prefix = (next->deleted > 0 ? "!" : "+");
  255.  
  256.       print_1_line (prefix, &files[1].linbuf[i]);
  257.     }
  258.     }
  259. }
  260.  
  261. /* Print a pair of line numbers with a comma, translated for file FILE.
  262.    If the second number is smaller, use the first in place of it.
  263.    If the numbers are equal, print just one number.
  264.  
  265.    Args A and B are internal line numbers.
  266.    We print the translated (real) line numbers.  */
  267.  
  268. static void
  269. print_unidiff_number_range (struct file_data const *file, lin a, lin b)
  270. {
  271.   long trans_a, trans_b;
  272.   translate_range (file, a, b, &trans_a, &trans_b);
  273.  
  274.   /* We can have B < A in the case of a range of no lines.
  275.      In this case, we should print the line number before the range,
  276.      which is B.  */
  277.   if (trans_b <= trans_a)
  278.     fprintf (outfile, trans_b < trans_a ? "%ld,0" : "%ld", trans_b);
  279.   else
  280.     fprintf (outfile, "%ld,%ld", trans_a, trans_b - trans_a + 1);
  281. }
  282.  
  283. /* Print a portion of an edit script in unidiff format.
  284.    HUNK is the beginning of the portion to be printed.
  285.    The end is marked by a `link' that has been nulled out.
  286.  
  287.    Prints out lines from both files, and precedes each
  288.    line with the appropriate flag-character.  */
  289.  
  290. static void
  291. pr_unidiff_hunk (struct change *hunk)
  292. {
  293.   lin first0, last0, first1, last1;
  294.   lin i, j, k;
  295.   struct change *next;
  296.   char const *function;
  297.   FILE *out;
  298.  
  299.   /* Determine range of line numbers involved in each file.  */
  300.  
  301.   if (! analyze_hunk (hunk, &first0, &last0, &first1, &last1))
  302.     return;
  303.  
  304.   /* Include a context's width before and after.  */
  305.  
  306.   i = - files[0].prefix_lines;
  307.   first0 = MAX (first0 - context, i);
  308.   first1 = MAX (first1 - context, i);
  309.   if (last0 < files[0].valid_lines - context)
  310.     last0 += context;
  311.   else
  312.     last0 = files[0].valid_lines - 1;
  313.   if (last1 < files[1].valid_lines - context)
  314.     last1 += context;
  315.   else
  316.     last1 = files[1].valid_lines - 1;
  317.  
  318.   /* If desired, find the preceding function definition line in file 0.  */
  319.   function = 0;
  320.   if (function_regexp.fastmap)
  321.     function = find_function (files[0].linbuf, first0);
  322.  
  323.   begin_output ();
  324.   out = outfile;
  325.  
  326.   fprintf (out, "@@ -");
  327.   print_unidiff_number_range (&files[0], first0, last0);
  328.   fprintf (out, " +");
  329.   print_unidiff_number_range (&files[1], first1, last1);
  330.   fprintf (out, " @@");
  331.  
  332.   if (function)
  333.     print_context_function (out, function);
  334.  
  335.   putc ('\n', out);
  336.  
  337.   next = hunk;
  338.   i = first0;
  339.   j = first1;
  340.  
  341.   while (i <= last0 || j <= last1)
  342.     {
  343.  
  344.       /* If the line isn't a difference, output the context from file 0. */
  345.  
  346.       if (!next || i < next->line0)
  347.     {
  348.       putc (initial_tab ? '\t' : ' ', out);
  349.       print_1_line (0, &files[0].linbuf[i++]);
  350.       j++;
  351.     }
  352.       else
  353.     {
  354.       /* For each difference, first output the deleted part. */
  355.  
  356.       k = next->deleted;
  357.       while (k--)
  358.         {
  359.           putc ('-', out);
  360.           if (initial_tab)
  361.         putc ('\t', out);
  362.           print_1_line (0, &files[0].linbuf[i++]);
  363.         }
  364.  
  365.       /* Then output the inserted part. */
  366.  
  367.       k = next->inserted;
  368.       while (k--)
  369.         {
  370.           putc ('+', out);
  371.           if (initial_tab)
  372.         putc ('\t', out);
  373.           print_1_line (0, &files[1].linbuf[j++]);
  374.         }
  375.  
  376.       /* We're done with this hunk, so on to the next! */
  377.  
  378.       next = next->link;
  379.     }
  380.     }
  381. }
  382.  
  383. /* Scan a (forward-ordered) edit script for the first place that more than
  384.    2*CONTEXT unchanged lines appear, and return a pointer
  385.    to the `struct change' for the last change before those lines.  */
  386.  
  387. static struct change *
  388. find_hunk (struct change *start)
  389. {
  390.   struct change *prev;
  391.   lin top0, top1;
  392.   lin thresh;
  393.  
  394.   /* Threshold distance is 2 * CONTEXT + 1 between two non-ignorable
  395.      changes, but only CONTEXT if one is ignorable.  Watch out for
  396.      integer overflow, though.  */
  397.   lin non_ignorable_threshold =
  398.     (LIN_MAX - 1) / 2 < context ? LIN_MAX : 2 * context + 1;
  399.   lin ignorable_threshold = context;
  400.  
  401.   do
  402.     {
  403.       /* Compute number of first line in each file beyond this changed.  */
  404.       top0 = start->line0 + start->deleted;
  405.       top1 = start->line1 + start->inserted;
  406.       prev = start;
  407.       start = start->link;
  408.       thresh = (prev->ignore || (start && start->ignore)
  409.         ? ignorable_threshold
  410.         : non_ignorable_threshold);
  411.       /* It is not supposed to matter which file we check in the end-test.
  412.      If it would matter, crash.  */
  413.       if (start && start->line0 - top0 != start->line1 - top1)
  414.     abort ();
  415.     } while (start
  416.          /* Keep going if less than THRESH lines
  417.         elapse before the affected line.  */
  418.          && start->line0 - top0 < thresh);
  419.  
  420.   return prev;
  421. }
  422.  
  423. /* Set the `ignore' flag properly in each change in SCRIPT.
  424.    It should be 1 if all the lines inserted or deleted in that change
  425.    are ignorable lines.  */
  426.  
  427. static void
  428. mark_ignorable (struct change *script)
  429. {
  430.   while (script)
  431.     {
  432.       struct change *next = script->link;
  433.       lin first0, last0, first1, last1;
  434.  
  435.       /* Turn this change into a hunk: detach it from the others.  */
  436.       script->link = 0;
  437.  
  438.       /* Determine whether this change is ignorable.  */
  439.       script->ignore = ! analyze_hunk (script,
  440.                        &first0, &last0, &first1, &last1);
  441.  
  442.       /* Reconnect the chain as before.  */
  443.       script->link = next;
  444.  
  445.       /* Advance to the following change.  */
  446.       script = next;
  447.     }
  448. }
  449.  
  450. /* Find the last function-header line in LINBUF prior to line number LINENUM.
  451.    This is a line containing a match for the regexp in `function_regexp'.
  452.    Return the address of the text, or 0 if no function-header is found.  */
  453.  
  454. static char const *
  455. find_function (char const * const *linbuf, lin linenum)
  456. {
  457.   lin i = linenum;
  458.   lin last = find_function_last_search;
  459.   find_function_last_search = i;
  460.  
  461.   while (last <= --i)
  462.     {
  463.       /* See if this line is what we want.  */
  464.       char const *line = linbuf[i];
  465.       size_t linelen = linbuf[i + 1] - line - 1;
  466.  
  467.       /* FIXME: re_search's size args should be size_t, not int.  */
  468.       int len = MIN (linelen, INT_MAX);
  469.  
  470.       if (0 <= re_search (&function_regexp, line, len, 0, len, 0))
  471.     {
  472.       find_function_last_match = i;
  473.       return line;
  474.     }
  475.     }
  476.   /* If we search back to where we started searching the previous time,
  477.      find the line we found last time.  */
  478.   if (find_function_last_match != LIN_MAX)
  479.     return linbuf[find_function_last_match];
  480.  
  481.   return 0;
  482. }
  483.